package com.zhuge_new.Activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.request.RequestOptions;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.zhuge_new.Base.BaseActivity;
import com.zhuge_new.R;
import com.zhuge_new.http.StringDialogCallback;
import com.zhuge_new.share.BaseHttp;
import com.zhuge_new.untils.ActivityStack;
import com.zhuge_new.untils.PreferencesUtils;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Administrator on 2018/9/30.
 */

public class PersonageActivity extends BaseActivity implements View.OnClickListener {

    private TextView person_petname;
    private ImageView iv_back;
    private ImageView person_back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personage);
        init_title();
        getData();
    }

    @Override
    public void init_title() {
        super.init_title();
        iv_back = findViewById(R.id.iv_nav_back);
        TextView tv_title=findViewById(R.id.tv_nav_title);
        TextView person_sex=findViewById(R.id.person_sex);
        TextView person_name=findViewById(R.id.person_name);
        TextView person_num=findViewById(R.id.person_num);
        person_petname = findViewById(R.id.person_petname);
        person_back = findViewById(R.id.person_back);
        tv_title.setText("个人资料");
        iv_back.setOnClickListener(this);
        String sex=PreferencesUtils.getString(baseContext,"sex");
        if (sex.equals("1")){
            person_sex.setText(R.string.sex1);
        }else {
            person_sex.setText(R.string.sex2);
        }
        RequestOptions options=new RequestOptions()
                .centerCrop()
                .placeholder(R.mipmap.ic_launcher_round)
                .placeholder(R.mipmap.ic_launcher_round)
                .priority(Priority.HIGH);
        person_name.setText(PreferencesUtils.getString(baseContext,"name"));
        person_num.setText(PreferencesUtils.getString(baseContext,"mobile"));
        Glide.with(baseContext).load(BaseHttp.baseImg1+PreferencesUtils.getString(baseContext,"userhead")).apply(options).into(person_back);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_nav_back:
                ActivityStack.getScreenManager().popOneActivity(PersonageActivity.class);
                break;
        }
    }

    public void getData() {
        OkGo.<String>post(BaseHttp.myProfile)
                .tag(this)
                .params("openid",PreferencesUtils.getString(baseContext,"openid"))
                .execute(new StringDialogCallback(baseContext) {
                    @Override
                    public void onSuccessResponse(Response<String> response, String msg, String msgCode) {
                        try {
                            JSONObject obj=new JSONObject(response.body()).getJSONObject("object");
                            String wxname=obj.getString("wxNickName");
                            person_petname.setText(wxname);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }
}
